package com.wanhe.shoppingcart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.wanhe.shoppingcart.bean.CartShowItem;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;

/**
 * Created by XieZaiWei on 2017/5/9.
 * 购物车adapter
 */

public class CartAdapter extends BaseExpandableListAdapter {
	protected Context mContext;
	protected LayoutInflater mLayoutInflater;
	protected ArrayList<CartShowItem> mArrCartShowItem;
	private int mGroupLayout;
	private int mItemLayout;

	public CartAdapter(Context context, int groupLayout, int itemLayout) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mGroupLayout = groupLayout;
		mItemLayout = itemLayout;
	}

	public void setData(ArrayList<CartShowItem> arrCartShowItem) {
		if(mArrCartShowItem != null){
			mArrCartShowItem.clear();
			mArrCartShowItem = null;
		}
		mArrCartShowItem = arrCartShowItem;
		notifyDataSetChanged();
	}

	public void clean(){
		if(mArrCartShowItem != null){
			mArrCartShowItem.clear();
			mArrCartShowItem = null;
		}
		notifyDataSetChanged();
	}

	@Override
	public int getGroupCount() {
		if(mArrCartShowItem == null){
			return 0;
		}
		return mArrCartShowItem.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public CartShowItem getGroup(int groupPosition) {
		return mArrCartShowItem.get(groupPosition);
	}

	@Override
	public ICartItem getChild(int groupPosition, int childPosition) {
		return mArrCartShowItem.get(groupPosition).getArrCartItem().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupHolder holder;
		if (convertView == null) {
			holder = new GroupHolder();
			convertView = mLayoutInflater.inflate(mGroupLayout, null);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);

		} else {
			holder = (GroupHolder) convertView.getTag();
		}
		holder.tv_title.setText(mArrCartShowItem.get(groupPosition).getTitle());

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolder holder;
		if (convertView == null) {
			holder = new ChildHolder();
			convertView = mLayoutInflater.inflate(R.layout.cart_child, null);
			holder.gv_dish = (GridView) convertView.findViewById(R.id.gv_dish);
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}
		holder.gv_dish.setAdapter(new ChildAdapter(mContext, mArrCartShowItem.get(groupPosition).getArrCartItem(), mItemLayout));

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}


	protected static class GroupHolder {
		public TextView tv_title;
	}

	protected static class ChildHolder {
		public GridView gv_dish;

	}
}
